import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { Observable } from 'rxjs';
import { Reflector } from '@nestjs/core';
import type { Request } from 'express';
@Injectable()
export class RoleGuard implements CanActivate {
  constructor(private Reflector:Reflector) { }
  canActivate(
    context: ExecutionContext,
  ): boolean | Promise<boolean> | Observable<boolean> {
    const ad = this.Reflector.get<string[]>('role', context.getHandler())
    const req = context.switchToHttp().getRequest<Request>()
    console.log('req.query', req.query)
    console.log('ad',ad)
    console.log('到达了守卫');
    if (ad.includes(req.query.role as string)) {
      console.log('经过了守卫');
      return true
    } else {
      console.log('被守卫拦下');
     return false
    }
  }
}
